import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import Login from "@/views/Login.vue"
// import Dashboard from "@components/Dashboard.vue"
Vue.use(VueRouter)
    // 重写push方法
let originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
    return originalPush.call(this, location).catch((err) => {
        console.log(err);
    })
}
const routes = [{
        path: '/',
        name: 'Home',
        component: Login
    },
    {
        path: '/dashboard',
        name: 'Dashboard',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "dashboard" */ '../components/Dashboard.vue'),
        children: [{
                path: "",
                component: () =>
                    import ("../components/Home"),
                meta: [{ name: "首页", to: "/dashboard" }]
            },
            {
                path: "/userlist",
                component: () =>
                    import ("../components/UserList"),
                meta: [{ name: "首页", to: "/dashboard" }, { name: "数据管理", to: "" }, { name: "用户列表", to: "" }]
            },
            {
                path: "/shoplist",
                component: () =>
                    import ("../components/ShopList"),
                meta: [{ name: "首页", to: "/dashboard" }, { name: "数据管理", to: "" }, { name: "商家列表", to: "" }]
            },
            {
                path: "/foodlist",
                component: () =>
                    import ("../components/FoodList"),
                meta: [{ name: "首页", to: "/dashboard" }, { name: "数据管理", to: "" }, { name: "食品列表", to: "" }]
            },
            {
                path: "/orderlist",
                component: () =>
                    import ("../components/OrderList"),
                meta: [{ name: "首页", to: "/dashboard" }, { name: "数据管理", to: "" }, { name: "订单列表", to: "" }]
            },
            {
                path: "/adminlist",
                component: () =>
                    import ("../components/AdminList"),
                meta: [{ name: "首页", to: "/dashboard" }, { name: "数据管理", to: "" }, { name: "管理员列表", to: "" }]
            },
            {
                path: "/addShop",
                component: () =>
                    import ("../components/AddShops"),
                meta: [{ name: "首页", to: "/dashboard" }, { name: "添加数据", to: "" }, { name: "添加商铺", to: "" }]
            },
            {
                path: "/addFood",
                component: () =>
                    import ("../components/AddShops"),
                meta: [{ name: "首页", to: "/dashboard" }, { name: "添加数据", to: "" }, { name: "添加商铺", to: "" }]
            },
        ]
    }
]

const router = new VueRouter({
    routes,
});
router.beforeEach(function(to, from, next) {
    console.log("全局导航守卫");
    let token = localStorage.getItem("token")
    if (token || to.path === '/') {
        next();
    } else {
        next('/');
    }
    next();
})

export default router